package cn.tedu.ivos.vehicle.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.vehicle.pojo.dto.AddVehicleParam;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVo;
import cn.tedu.ivos.vehicle.service.VehicleService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/v1/vehicle")
@Api(tags = "车辆管理控制器")
public class VehicleController {
    @Autowired
    VehicleService vehicleService;
    @GetMapping("/select")
    public JsonResult selectVehicleTab(VehicleQuery vehicleQuery){
        log.debug("selectVehicleTable"+vehicleQuery);
        List<VehicleVo> vehicleList = vehicleService.selectVehicleTableSer(vehicleQuery);
        return JsonResult.ok(vehicleList);
    }
    @PostMapping("/add")
    public JsonResult addVehicle(AddVehicleParam addVehicleParam){
        log.debug("addVehicleParam: {}",addVehicleParam);
        int row = vehicleService.addVehicleSer(addVehicleParam);
        return JsonResult.ok("成功");
    }
    @PostMapping("/unbind/{vehicleId}")
    public JsonResult unbindVehicle(@PathVariable Long vehicleId){
        log.debug("id: {}",vehicleId);
        int row = vehicleService.unbindVehicleSer(vehicleId);
        if(row==0){
            return new JsonResult(StatusCode.OPERATION_FAILED,"解绑失败");
        }
        return JsonResult.ok("解绑成功");
    }
    //bind
    @PostMapping("/bind/{vehicleId}/{geofenceId}")
    public JsonResult bindVehicle(@PathVariable Long vehicleId,@PathVariable Long geofenceId){
        log.debug("bindVehicle: {}",vehicleId);
        log.debug("bindVehicle: {}",geofenceId);
        int row = vehicleService.bindVehicleSer(vehicleId,geofenceId);
        if(row==0){
            return new JsonResult(StatusCode.OPERATION_FAILED,"绑定失败");
        }
        return JsonResult.ok("绑定成功");
    }
    //删除
    @PostMapping("/delete/{id}")
    public JsonResult deleteVehicle( @PathVariable Long id){
        log.debug("deleteVehicle: {}",id);
        int row = vehicleService.deleteVehicleSer(id);
        if (row==0) {
            return JsonResult.ok("删除成功");
        }else {
            return JsonResult.ok("删除失败");
        }
    }
}
